/* ************************************************************************

   Copyright:

   License:

   Authors: SALMON Charles

************************************************************************ */

/**
* The User object contains information on the connected user
 * It propose also some statics methods to retrieve users informations
 */
qx.Class.define("bip.rpc.User", 
{
	extend : qx.core.Object,
	
	type: "singleton",
	
	/*
	*****************************************************************************
	PROPERTIES
	*****************************************************************************
	*/ 
	properties : {
		"id": 
		{
			check:"Integer",
			nullable:true	
		},
		
		"name": 
		{
			check:"String",
			event: "changeName",
			nullable:true	
		}
		
	},
  
  
  	/*
	*****************************************************************************
	 CONSTRUCTOR
	*****************************************************************************
	*/
	
	/**
	 * Connect to the JSON-RPC server and synchronize
	 * client side object properties with server side ones
	 */	
 	construct: function()
	{
		this.base(arguments);
		try {
			this._loadHandler(bip.rpc.RpcManager.getInstance().getJsonRpc().Session.getUsername(),null);
		} catch(e) {
			this._loadHandler(null,e);
		}
	},

  	/*
	*****************************************************************************
	 MEMBERS
	*****************************************************************************
	*/	
	members : {
		__dataUsersList : null,
		
		/**
		 * Get back users list
		 * NOTE: the method is executed synchronously !
		 * 
		 * @param refresh {Boolean} Force cache update
		 * 
		 * @return {qx.data.Array} The users list
		 */
		getUsersList: function(refresh) {
			if (this.__dataUsersList == null || refresh) {
				try {
					var res = bip.rpc.RpcManager.getInstance().getJsonRpc().UserManager.getAllUserNames();
					if (this.__dataUsersList == null) {
						this.__dataUsersList = new qx.data.Array();
					}
					this.__dataUsersList.removeAll();
					this.__dataUsersList.append(res.list);
				} catch (e) {
					alert("Can not get users list:"+e);
				}
			}
			return this.__dataUsersList;
		},
		
		/**
		 * Reload the user properties
		 * @return {Integer} The request id
		 */
		reload: function() {
			var self = this;
			return bip.rpc.RpcManager.getInstance().getJsonRpc().Seesion.getUsername(function(result,error) {self._loadHandler.apply(self,arguments);});
		},
		
		_loadHandler: function(res,error) {
			if (res == null) {
				alert("Can not load User:"+error);
				return;
			}
		
			//this.setId(res.id);
			this.setName(res);
		}
	
	}
  
});
